<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-input
          placeholder="请输入内容"
          v-model="search"
          class="input-with-select"
          style="width: 400px"
        >
          <el-button slot="append" icon="el-icon-search" @click="toSearch"></el-button>
        </el-input>
        <!-- <el-button type="primary" size="small" style="float: right">新增</el-button> -->
      </div>
      <div class="text item">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="姓名" width="120px" prop="name">
          </el-table-column>
          <el-table-column label="手机号" width="" prop="mobile">
          </el-table-column>
          <el-table-column label="打卡地点" width="" prop="position">
          </el-table-column>
          <el-table-column label="打卡时间" width="" prop="time">
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 50]"
          :page-size="10"
          :current-page.sync="currentPage"
          style="float: right; margin: 20px 0"
          background
          layout="sizes, prev, pager, next"
          :total="10"
        >
        </el-pagination>
        <el-dialog title="修改信息" :visible.sync="dialogFormVisible" :append-to-body='true'>
          <el-form :model="form">
            <el-form-item label="打卡人" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off" :disabled='true'></el-input>
            </el-form-item>
            <el-form-item label="手机号" :label-width="formLabelWidth">
              <el-input v-model="form.mobile" autocomplete="off" :disabled='true'></el-input>
            </el-form-item>
            <el-form-item label="打卡地点" :label-width="formLabelWidth">
              <el-input v-model="form.position" autocomplete="off" :disabled='false'></el-input>
            </el-form-item>
            <el-form-item label="打卡时间" :label-width="formLabelWidth">
              <el-input v-model="signInTime" :disabled='true' autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateInfo">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getSignIn, getSignInmohu, updateSignIn, deleteSignIn } from '@/api/signIn'
export default {
  name: '',
  components: {},
  props: {},
  data () {
    return {
      search: '',
      currentPage: 1,
      pageSize: 10,
      total: 100,
      tableData: [],
      form: {},
      signInTime: '',
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {
    this.getAllInfo()
  },
  methods: {
    handleEdit (index, row) {
      console.log(index, row)
      this.form = row
      this.signInTime = new Date(row.time_stamp).toLocaleString()
      this.dialogFormVisible = true
    },
    handleDelete (index, row) {
      console.log(index, row)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await deleteSignIn(row)
        console.log(res)
        if (res.data.errno === 1000) {
          this.getAllInfo()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
          this.$message({
            type: 'warning',
            message: '删除失败!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async updateInfo () {
      const res = await updateSignIn(this.form)
      console.log('update res', res)
      if (res.data.errno === 1000) {
        this.$message({
          message: '修改成功',
          type: 'success'
        })
      } else {
        this.$message({
          message: '修改失败',
          type: 'warning'
        })
      }
      this.dialogFormVisible = false
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
    },
    async getAllInfo () {
      const res = await getSignIn()
      console.log('res', res)
      this.tableData = res.data.data
    },
    async toSearch () {
      console.log('this.search', this.search)
      if (this.search) {
        const res = await getSignInmohu({
          name: this.search
        })
        console.log(res)
        this.tableData = res.data.data
      } else {
        const res = await getSignIn()
        console.log(res)
        this.tableData = res.data.data
      }
    }
  }
}
</script>

<style scoped lang="">
</style>
